// ignore_for_file: prefer_const_constructors
import 'package:flutter/material.dart';
import 'package:get/get.dart';

/// 分类页面，左侧主分类，右侧子分类展示
class Classification extends StatefulWidget {
  const Classification({Key? key}) : super(key: key);

  @override
  State<Classification> createState() => _ClassificationState();
}

class _ClassificationState extends State<Classification> {
  // 模拟主分类数据
  final List<String> _categories = [
    '推荐',
    '女装',
    '男装',
    '美妆',
    '家居',
    '数码',
    '母婴',
    '食品',
  ];
  // 模拟子分类数据
  final Map<String, List<String>> _subCategories = {
    '推荐': ['热门', '新品', '秒杀', '特价'],
    '女装': ['连衣裙', '上衣', '裤子', '外套', '内衣'],
    '男装': ['T恤', '衬衫', '牛仔', '外套', '运动装'],
    '美妆': ['口红', '粉底', '护肤', '香水'],
    '家居': ['家具', '床品', '装饰', '收纳'],
    '数码': ['手机', '电脑', '相机', '耳机'],
    '母婴': ['奶粉', '纸尿裤', '玩具', '童装'],
    '食品': ['零食', '饮料', '干货', '生鲜'],
  };

  int _selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    String currentCategory = _categories[_selectedIndex];
    List<String> children = _subCategories[currentCategory] ?? [];

    return Scaffold(
      appBar: AppBar(title: Text('分类')),
      body: Row(
        children: [
          // 左侧主分类
          Container(
            width: 100,
            color: Colors.grey[100],
            child: ListView.builder(
              itemCount: _categories.length,
              itemBuilder: (context, index) {
                bool selected = index == _selectedIndex;
                return InkWell(
                  onTap: () => setState(() => _selectedIndex = index),
                  child: Container(
                    height: 50,
                    alignment: Alignment.center,
                    color: selected ? Colors.white : Colors.grey[100],
                    child: Text(
                      _categories[index],
                      style: TextStyle(
                        fontWeight:
                            selected ? FontWeight.bold : FontWeight.normal,
                        color: selected ? Colors.teal : Colors.black87,
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
          // 右侧子分类
          Expanded(
            child: Container(
              color: Colors.white,
              padding: EdgeInsets.all(8),
              child: GridView.builder(
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  crossAxisSpacing: 8,
                  mainAxisSpacing: 8,
                  childAspectRatio: 3 / 2,
                ),
                itemCount: children.length,
                itemBuilder: (context, idx) {
                  return InkWell(
                    onTap: () {
                      // TODO: 点击子分类，跳转或过滤
                      Get.toNamed('/category_products', arguments: {
                        'category': currentCategory,
                        'subCategory': children[idx],
                      });
                      debugPrint('点击 ${children[idx]}');
                    },
                    child: Container(
                      decoration: BoxDecoration(
                        color: Colors.grey[50],
                        borderRadius: BorderRadius.circular(6),
                        border: Border.all(color: Colors.grey.shade300),
                      ),
                      alignment: Alignment.center,
                      child: Text(
                        children[idx],
                        style: TextStyle(fontSize: 14),
                      ),
                    ),
                  );
                },
              ),
            ),
          ),
        ],
      ),
    );
  }
}
